<template>
  <div class="app-page">
    <el-table-form ref="form" :model="query">
      <el-form-item class="merge-item">
        <el-select v-model="csmType" style="width: 170px; margin-right: 2px;">
          <el-option label="用户名称" value="userName" />
          <el-option label="用户编号" value="userCode" />
          <el-option label="用户电话" value="uphone" />
        </el-select>
        <el-input v-model="csmText" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="所属机构" prop="orgSNNo">
        <el-tree-select
          v-model="query.orgSNNo"
          clearable
          class="tree-select"
          :popover-width="360"
          :select-params="{
            clearable: true,
            placeholder: '请选择',
            loading: $loadingPlugin.getTreeData
          }"
          :tree-params=" {
            'default-expand-all': true,
            'expand-on-click-node': false,
            'highlight-current': true,
            props:{
              value:'orgSNNo',
              label: 'orgName',
              disabled: 'disabled',
            },
            data: treeData
          }"
        />
      </el-form-item>
      <el-form-item label="上报时间:">
        <el-date-picker
          v-model="date"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item label="排序:">
        <el-select v-model="query.orderBy" clearable filterable placeholder="请选择">
          <el-option :value="1" label="按版本号由高到低" />
          <el-option :value="2" label="按版本号由低到高" />
        </el-select>
      </el-form-item>
      <template v-slot:btns>
        <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
      </template>
    </el-table-form>
    <lb-table
      ref="lbTable"
      v-loading="$loadingPlugin.fetchData"
      v-el-height-adaptive-table="{bottomOffset:10}"
      border
      highlight-current-row
      element-loading-text="加载中"
      :column="tableData.column"
      :data="tableData.data"
      pagination
      :total="tableData.total"
      custom-column
      @fetch="fetchData"
    />
  </div>
</template>

<script>
  import { queryAppVersionList } from '@/api'
  import { parseTime } from '@/utils'
  import getTreeData from '@/utils/jsonTree'
  import { mapGetters } from 'vuex'
  import moment from 'dayjs'

  export default {
    name: 'appVersionRecord',
    data() {
      return {
        treeData: [],
        csmType: 'userName',
        csmText: '',
        date: [],
        query: {
          orgSNNo: '', // 所属机构orgSNNo
          orderBy: 1
        },
        tableData: {
          total: 0,
          column: [
            {
              type: 'index'
            },
            {
              prop: 'userName',
              label: '用户名称'
            },
            {
              prop: 'userCode',
              label: '用户编号'
            },
            {
              prop: 'uphone',
              label: '用户电话',
              width: 110
            },
            {
              prop: 'orgName',
              label: '所属机构',
              width: 150
            },
            {
              prop: 'appVersion',
              label: '软件版本'
            },
            {
              prop: 'buildTime',
              label: '编译时间',
              width: 150,
              render: (h, { row }) => <span>{ row.buildTime && parseTime(row.buildTime, '{y}-{m}-{d} {h}:{i}') }</span>
            },
            {
              prop: 'phoneName',
              label: '手机品牌'
            },
            {
              prop: 'phoneModel',
              label: '手机型号',
              width: 120
            },
            {
              prop: 'nativeOSName',
              label: '操作系统'
            },
            {
              prop: 'nativeOSVersion',
              width: 120,
              label: '操作系统版本'
            },
            {
              prop: 'modifyTime',
              label: '上报时间',
              width: 150,
              render: (h, { row }) => <span>{ row.modifyTime && parseTime(row.modifyTime, '{y}-{m}-{d} {h}:{i}') }</span>
            },
            {
              prop: 'mac1',
              width: 150,
              label: '手机mac'
            },
            {
              prop: 'mac2',
              label: 'wifi mac',
              width: 150
            }
          ],
          data: []
        }
      }
    },
    computed: {
      ...mapGetters([
        'orgList'
      ])
    },
    async created() {
      await this.getTreeData()
    },
    methods: {
      // 获取所属机构数据
      async getTreeData() {
        const data = this.orgList.map(item => ({
          label: item.orgName,
          ...item
        }))
        this.treeData = getTreeData(data)
      },
      _getQuery(obj) {
        const query = {}
        for (const key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (obj[key] !== '') {
              query[key] = obj[key]
            }
          }
        }
        return query
      },
      async fetchData({ currentPage, pageSize }) {
        const query = {
          ...this._getQuery(this.query),
          currentPage,
          pageSize
        }
        if (this.date && this.date.length > 0) {
          query.modifyTime = moment(this.date[0]).format()
          query.modifyTimeEnd = moment(this.date[1]).format()
        }
        query[this.csmType] = this.csmText
        const res = await queryAppVersionList(query)
        this.tableData.data = res.data.rows
        if (currentPage === 1) this.tableData.total = res.data.total
      },
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      }
    }
  }
</script>

<style lang="scss" scoped>
.el-form-item .el-form-item {
  margin-bottom: 0;
}
.merge-item {
  /deep/ .el-form-item__content {
    display: flex;
  }
}
.btn-group {
  display: flex;
  justify-content: space-between;
}
</style>
